<style>
    /*.layui-input-inline{*/
    /*    width: 160px;*/
    /*}*/
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <form class="layui-form" lay-filter="demo-val-filter">
                <div class="layui-form lay-header">
                    <div class="toolbar">
                        <div class="layui-form-item lay-query-content">
                            <div class="lay-query-box">
                                <div class="query-lt" >
                                    <div class="layui-inline">
                                        <label class="layui-form-label">模板名称：</label>
                                        <div class="layui-input-inline">
                                            <input name="name" class="layui-input" type="text"
                                                   placeholder="请输入模板名称"/>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label">所属物料类型：</label>
                                        <div class="layui-input-inline">
                                            <div id="productTypeNameSearch"
                                                 lay-filter="productTypeNameSearch">
                                            </div>
                                            <input id="productTypeId" name="productTypeId" type="hidden">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label">质检性质：</label>
                                        <div class="layui-input-inline">
                                            <select id="qualityTestingNatureFormId" name="qualityTestingNatureFormId"
                                                    lay-filter="qualityTestingNatureFormId"
                                                    class="common-select-search">
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label">质检类型：</label>
                                        <div class="layui-input-inline">
                                            <select id="qualityTestingTypeId" name="qualityTestingTypeId"
                                                    class="common-select-search"/>
                                        </div>
                                    </div>

                                </div>
                                <div class="query-rt">
                                    <div class="layui-inline">
                                        <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                                lay-submit type="button" >查询
                                        </button>
                                        <button class="layui-btn icon-btn color-white"
                                                lay-filter="resets"
                                                lay-submit
                                                type="button">重置
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="lay-btn-rows">
                        <div class="lay-btn-box">
                            <div class="lay-btn-lt">
                                <button id="qualityTemplateBtnAdd" class="layui-btn icon-btn color-green"
                                        type="button">
                                    添加质检模板
                                </button>
                            </div>
                            <div class="lay-btn-rt">
                            </div>
                        </div>
                    </div>
                </div>
            </form>
            <!-- 表格 -->
            <table class="layui-table" id="qualityTemplateTable" lay-filter="qualityTemplateTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="qualityTemplateTableBar">
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="edit">修改</a>
<!--    <a class="layui-btn layui-btn-xs" lay-event="details">详情</a>-->
    <a class="layui-btn layui-btn-xs" lay-event="print"
       style="background:{{d.isDeleted?'':'red'}}">{{d.isDeleted?'恢复':'废除'}}</a>
</script>


<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        //初始化查询的form
     let treeddd=   admin.initDataSelectTree(true, 'enterprise/product/admin/productType/getTreeData', {materialNatureList: [2, 3, 7]}, 'productTypeNameSearch', null, "物料分类", "productTypeId", null);
        console.log('treeddd',treeddd)
        //质检性质
        admin.getDictList("qualityTestingNatureFormId", "pro_quality_testing_nature", null, "质检性质");
        //送检类型
        admin.initDataSelectAndMap('production/qualityTesting/admin/qualityTestingType/list', {limit: 9999}, "qualityTestingTypeId", null, "送检类型", "name", "id", null);

        form.render('select');

        $("#qualityTemplateBtnAdd").click(function () {
            showEditModel(null);
        })

        // 渲染表格
        var insTb = table.render({
            elem: '#qualityTemplateTable',
            url: config.base + 'enterprise/quality/admin/qualityTemplate/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号', fixed: 'center'}
                , {field: 'id',align: 'center', title: '质检模板id', fixed: 'center',width: 100  }
                , {field: 'name', title: '质检模板名称', fixed: 'center'}
                , {field: 'qualityTestingTypeName', title: '质检类型', fixed: 'center'}
                , {field: 'productNatureName', title: '质检性质', fixed: 'center'}
                , {field: 'materialTypeName', title: '所属物料类型', fixed: 'center'}
                , {field: 'remark', title: '描述', fixed: 'center'}
                , {
                    field: 'isDeleted', title: '状态', fixed: 'center',width: 68,
                    templet: function (d) {
                        return d.isDeleted ? '<span style="color:red">异常</span>' : '<span >正常</span>'
                    }
                }
                , {field: 'creatorName', title: '操作人', fixed: 'center'}
                , {field: 'createTime', title: '添加时间', fixed: 'center'}
                , {align: 'center', toolbar: '#qualityTemplateTableBar', title: '操作', width:180}
            ]],
            done: function (res, curr, count) {
                //移除按钮
            }
        });

        // 工具条点击事件
        table.on('tool(qualityTemplateTable)', function (obj) {
            let data = obj.data;
            if (obj.event === 'details') { //详情
                showDetailsModel(data);
            } else if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'print') { // 打印
                getPrintBarcode(data)
            }
        });

        function getPrintBarcode(data) {
            const status=data.isDeleted?'1':'2'
            const  templateId=data.id
            console.log('templateId',templateId )
            admin.req(`enterprise/quality/admin/qualityTemplate/disableOrOpen/${templateId}/${status}`,null,function (res){
                table.reload('qualityTemplateTable');

            },'PUT')
        }

        /**
         * 弹出修改框
         * @param data
         */
        function showEditModel(data) {
            // if (data){
            //     admin.putTempData("taskQualityTemplateData",data);
            // }else{
            //     admin.putTempData("taskQualityTemplateData",null);
            // }
            admin.formOpen({
                type: 1,
                area: ['75%', '80%'],
                style: 'font-size: 20px;',
                title: data ? '修改质检模板' : '添加质检模板',
                path: 'components/res/quality/qualityTemplateForm.html',
                success: function () {
                    data ? window.parentData.id = data.id : ''
                },
                finish: function () {
                    console.log('22222')
                    // table.reload('qualityTemplateTable');
                },
                end: function (data) {
                    console.log(data)
                    // table.reload('qualityTemplateTable');
                }
            });
        }

        //监听排序
        table.on('sort(qualityTemplateTable)', function (obj) {
            table.reload('qualityTemplateTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            console.log(123)
            const formData={
                productNatureId:data.field.qualityTestingNatureFormId,
                materialTypeId:data.field.productTypeId,
                ...data.field
            }
            insTb.reload({where:formData, page: {curr: 1}}, 'data');
        });
        form.on('submit(resets)', function (data) {
            xmSelect.get('#productTypeNameSearch')[0].setValue([])
            let field = data.field;
            field.productNatureId='';
            field.materialTypeId='';
            console.log(data)
            insTb.reload({where: admin.resetSearch(field), page: {curr: 1}}, 'data');
        });
    });
</script>